package probe.logic;

/*
 * <task>
 * <taskname> ... </taskname>
 * <date> ... </date>
 * <time> ... </time>
 * <date> ... </date>
 * <time> ... </time>
 * <type> ... </type>
 * </task>
 */

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class XML {

	private static final String fileName = "test.xml";

	private static final String taskBeginIdentifier = "<task>";
	private static final String taskEndIdentifier = "</task>";

	private static final String nameBeginIdentifier = "<name>";
	private static final String nameEndIdentifier = "</name>";

	private static final String dateBeginIdentifier = "<date>";
	private static final String dateEndIdentifier = "</date>";

	private static final String timeBeginIdentifier = "<time>";
	private static final String timeEndIdentifier = "</time>";

	private static final String statusBeginIdentifier = "<status>";
	private static final String statusEndIdentifier = "</status>";

	public static ArrayList<Task> getListOfTasksFromXml() throws IOException {

		ArrayList<Task> listTask = new ArrayList<Task>();

		BufferedReader reader = new BufferedReader(new FileReader(fileName));
		String line = "";

		while ((line = reader.readLine()) != null) {
			if (!line.equalsIgnoreCase(taskBeginIdentifier))
				continue;

			String name = null;
			Date startingDate = null;
			Date endingDate = null;
			Time startingTime = null;
			Time endingTime = null;
			Date deadline = null;
			Time time = null;
			Task.TaskStatus taskStatus = null;

			while ((line = reader.readLine()) != null
					&& !line.equalsIgnoreCase(taskEndIdentifier)) {

				String currentName = getTaskName(line);
				Date currentDate = getTaskDate(line);
				Time currentTime = getTaskTime(line);
				Task.TaskStatus currentStatus = getTaskStatus(line);

				if (currentName != null)
					name = currentName;

				if (currentDate != null) {
					if (deadline == null)
						deadline = currentDate;
					else {
						startingDate = deadline;
						endingDate = currentDate;
					}				
				}
				
				if (currentTime != null) {
					if (time == null)
						time = currentTime;
					else {
						startingTime = time;
						endingTime = currentTime;
					}
				}

				if (currentStatus != null)
					taskStatus = currentStatus;
			}

			if (name != null && taskStatus != null) {
				Task currentTask;
				if (startingDate != null && startingTime != null
						&& endingDate != null && endingTime != null)
					currentTask = new Task(name, startingDate, endingDate,
							startingTime, endingTime, taskStatus);
				else if (deadline != null && time != null)
					currentTask = new Task(name, deadline, time, taskStatus);
				else
					currentTask = new Task(name, taskStatus);

				listTask.add(currentTask);
			}
		}
		reader.close();
		return listTask;
	}

	/*
	 * public static void main(String[] args) throws IOException {
	 * 
	 * ArrayList listTask = getListOfTasksFromXml();
	 * 
	 * for (int i = 0; i < listTask.size(); i++)
	 * System.out.println(listTask.get(i));
	 * 
	 * exportListOfTasksToXml(listTask); }
	 */

	private static String getTaskName(String line) {
		return getTaskComponent(line, nameBeginIdentifier, nameEndIdentifier);
	}

	private static Date getTaskDate(String line) {
		String dateString = getTaskComponent(line, dateBeginIdentifier,
				dateEndIdentifier);

		if (dateString == null) {
			return null;
		} else {
			return new Date(dateString);
		}
	}

	private static Time getTaskTime(String line) {
		String timeString = getTaskComponent(line, timeBeginIdentifier,
				timeEndIdentifier);
		
		if (timeString == null) {
			return null;
		} else {
			return new Time(timeString);
		}
	}

	private static Task.TaskStatus getTaskStatus(String line) {
		return Task.findTaskStatus(getTaskComponent(line,
				statusBeginIdentifier, statusEndIdentifier));
	}

	private static String getTaskComponent(String line, String beginIdentifier,
			String endIdentifier) {

		int indexOfBeginIdentifier = line.indexOf(beginIdentifier);
		int indexOfEndIdentifier = line.indexOf(endIdentifier);

		if (indexOfBeginIdentifier == -1 || indexOfEndIdentifier == -1
				|| indexOfBeginIdentifier >= indexOfEndIdentifier)
			return null;

		String taskComponent = "";
		for (int i = indexOfBeginIdentifier + beginIdentifier.length(); i < indexOfEndIdentifier; i++)
			taskComponent += line.charAt(i);

		return taskComponent;
	}

	public static void exportListOfTasksToXml(ArrayList<Task> listTask)
			throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));

		for (Task task : listTask) {

			String taskName = task.taskName;
			Date startingDate = task.startingDate;
			Date endingDate = task.endingDate;
			Time startingTime = task.startingTime;
			Time endingTime = task.endingTime;
			Date deadline = task.deadline;
			Time time = task.time;
			Task.TaskStatus taskStatus = task.taskStatus;

			writer.write(taskBeginIdentifier + "\n");
			writer.write(nameBeginIdentifier + taskName + nameEndIdentifier
					+ "\n");
			writer.write(statusBeginIdentifier + taskStatus
					+ statusEndIdentifier + "\n");
			if (startingDate != null && endingDate != null
					&& startingTime != null && endingTime != null) {
				writer.write(dateBeginIdentifier + startingDate
						+ dateEndIdentifier + "\n");
				writer.write(timeBeginIdentifier + startingTime
						+ timeEndIdentifier + "\n");
				writer.write(dateBeginIdentifier + startingDate
						+ dateEndIdentifier + "\n");
				writer.write(timeBeginIdentifier + endingTime
						+ timeEndIdentifier + "\n");
			} else if (deadline != null && time != null) {
				writer.write(dateBeginIdentifier + deadline + dateEndIdentifier
						+ "\n");
				writer.write(timeBeginIdentifier + time + timeEndIdentifier
						+ "\n");

			}
			writer.write(taskEndIdentifier + "\n");
			writer.write("\n");
		}
		writer.close();
	}
}
